package com.swanclouds.redfish.systems;

import com.google.common.base.Preconditions;
import com.swanclouds.api.Apis;
import com.swanclouds.api.chassis.DrivesService;
import com.swanclouds.api.systems.StorageService;
import com.swanclouds.api.systems.VolumeService;
import com.swanclouds.core.transport.ClientConstants;
import com.swanclouds.model.common.redfish.CollectionResource;
import com.swanclouds.redfish.chassis.DrivesServiceImpl;
import com.swanclouds.redfish.common.redfish.CollectionResourceModel;
import com.swanclouds.redfish.internal.BaseSystemService;
import com.swanclouds.redfish.systems.domain.StorageModel;

import java.util.ArrayList;
import java.util.List;

/**
 * 查询存储集合资源信息服务
 *
 * @author:      张立强
 *  
 */
public class StorageServiceImpl extends BaseSystemService implements StorageService {

    private DrivesService drivesService = new DrivesServiceImpl();
    /**
     * 查询存储集合资源信息
     * @return CollectionResource
     */
    @Override
    public CollectionResource get() {
        return get(CollectionResourceModel.class,uri(ClientConstants.SYSTEMS_STORAGES, getSystemId())).execute();
    }

    /**
     * 查询指定存储控制器信息
     *
     * @param storageId 存储器ID
     * @return Storage
     */
    @Override
    public StorageModel getStorage(String storageId) {
        Preconditions.checkNotNull(storageId, "Storage Id is null");
        return get(StorageModel.class,uri(ClientConstants.SYSTEMS_STORAGES_ID, getSystemId(), storageId)).execute();
    }

    /**
     * 查询存储控制器资源信息列表
     * @return List&lt;Storage&gt;
     */
    @Override
    public List<StorageModel> list() {
        List<StorageModel> storages = new ArrayList<>();
        CollectionResource resource = get();
        resource.getCollectionResourceList().stream().forEach(ref -> {
            storages.add(get(StorageModel.class, uri(ref)).execute());
        });
        return storages;
    }

    @Override
    public boolean supertVolumes() {
        CollectionResource resource = get();
        return resource.getMemberCount() != null && resource.getMemberCount() > 0 ? true : false;
    }

    @Override
    public VolumeService Volumes() {
        return Apis.get(VolumeService.class);
    }

    @Override
    public boolean suppertDrives() {
        CollectionResource resource = drivesService.get();
        return resource.getCollectionResourceList() != null && resource.getCollectionResourceList().size() > 0 ? true : false;
    }

    @Override
    public DrivesService Drives() {
        return Apis.get(DrivesService.class);
    }

}
